package Providers
{
	import Model.*;
	
	import SharedCloud.Cloud;
	
	import flash.events.Event;
	
	import mx.collections.ArrayCollection;
	import mx.core.Application;
	import mx.rpc.remoting.mxml.RemoteObject;
	
	public class DataProvider
	{
		private var cloud:Cloud;
		
		private var _project:Project = null;
		[Bindable]
		public function get project():Project
		{
			if(!cloud)
			{
				cloud = new Cloud();
				
				var ro:RemoteObject = new RemoteObject();
				ro.destination = "fluorine";
				ro.requestTimeout = 300;
				ro.endpoint = Application.application.parameters.rootURL + "/Gateway.aspx";
				ro.source = "CerasusServices.WorkspaceService";
				cloud.connect(ro);
				cloud.addEventListener("InitialUpdateComplete", updateCompleteHandler);
			}
			return _project;
		}	
		protected function set project(val:Project):void
		{
			_project = val;
		}	
		
		protected function updateCompleteHandler(evt:Event):void
		{
			var projects:ArrayCollection = cloud.find(Project);
			if(projects.length == 0)
			{
				var np:Project = new Project();
				np.archiveBacklog = new Backlog();
				np.sprintBacklog = new Backlog();
				np.config = new Config();
				cloud.update(np);
				project = np;
			}
			else
			{
				project = projects[0];
			}
		}
	}
}